package com.areaCalc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SaveActivity extends Activity {
	private Double value;
	private AreaCalcHelper helper;
	private TextView valueView;
	private EditText name;
	private String type;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.save_layout);
		helper = new AreaCalcHelper(this);
		if(getIntent().getExtras() != null) {
			value = getIntent().getExtras().getDouble("Value");
		}
		TextView label = (TextView) findViewById(R.id.text);
		if(getIntent().getExtras().getBoolean("isArea")) {
			label.setText("Your area is: ");
			type="Area";
		}else {
			label.setText("Your distance is: ");
			type="Distance";
		}
		valueView = (TextView) findViewById(R.id.area_view);
		valueView.setText(value.toString());
		name = (EditText)findViewById(R.id.name);
		Button save = (Button) findViewById(R.id.save);
		save.setOnClickListener(onSave);

	}

	View.OnClickListener onSave = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			helper.insert(name.getText().toString(),
					valueView.getText().toString(), type);
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(this).inflate(R.menu.list, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId()==R.id.list_menu) {
			Intent listAct = new Intent(SaveActivity.this, CalculationList.class);
			startActivity(listAct);
			return(true);
		}
		return super.onOptionsItemSelected(item);
	}
}
